package com.ctsi.ssdc.controller;

import com.ctsi.ssdc.entity.*;
import com.ctsi.ssdc.service.*;
import com.ctsi.ssdc.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/excel")
public class ExcelController {


    @Resource
    private ExcelService excelService;

    /**
     * 获取最新有数据日期
     * @return map -> {
     *     time
     *     year
     *     month
     * }
     */
    @ApiOperation("获取最新有数据日期")
    @GetMapping("/getLatestDataTime")
    public Map<String, Object> getLatestDataTime() {
        Map<String, Object> res = new HashMap();
        Date date = excelService.getLatestDataTime();
        res.put("date", TimeUtil.toString(date));
        res.put("year", TimeUtil.year(date));
        res.put("month", TimeUtil.month(date));
        return res;
    }

    /**
     * 上传excel至数据库
     *
     * @param file excel文件
     * @param date 日期 yyyy-MM
     * @return
     */
    @ApiOperation("上传excel至数据库")
    @PostMapping("/uploadExcel")
    public Boolean uploadExcel(@RequestParam(value = "file") MultipartFile file,
                               @DateTimeFormat(pattern = "yyyy-MM") @RequestParam(value = "date") Date date) {
        return excelService.uploadExcel(file, date);
    }

    /**
     * 上传excel至数据库(覆盖以前数据)
     *
     * @param file excel文件
     * @param date 日期 yyyy-MM
     * @return
     */
    @ApiOperation("上传excel至数据库(覆盖以前数据)")
    @PostMapping("/uploadExcelConvert")
    public Boolean uploadExcelConvert(@RequestParam(value = "file") MultipartFile file,
                                      @DateTimeFormat(pattern = "yyyy-MM") @RequestParam(value = "date") Date date) {
        excelService.removeAllExcel(date);
        return excelService.uploadExcel(file, date);
    }


    /**
     * 返回当前excel月份是否重复
     *
     * @param date 年月
     * @return true 存在 false 不存在
     */
    @ApiOperation("返回当前excel月份是否重复")
    @GetMapping("/checkExcel/{date}")
    public Boolean checkExcel(@DateTimeFormat(pattern = "yyyy-MM") @PathVariable("date") Date date) {
        return excelService.checkExcel(date);
    }

    /**
     * 插入以前主要经济指标专用
     * @param file 文件
     * @param date 日期
     * @return
     */
    @ApiOperation("插入以前主要经济指标专用")
    @PostMapping("/importFormerData")
    public Boolean importFormerData(@RequestParam(value = "file") MultipartFile file,
                                    @DateTimeFormat(pattern = "yyyy-MM") @RequestParam(value = "date") Date date) {
        return excelService.importFormerData(file, date);
    }


    @ApiOperation("更新数据")
    @PutMapping("/updateExcelData/{year}")
    public Boolean updateExcelData(@PathVariable Integer year) {
        for (int i = 2; i <= 12; i++) {
            excelService.calculateExcel(TimeUtil.date(year, i));
        }
        return true;
    }

    @ApiOperation("更新一年外贸的数据")
    @PutMapping("/updateSheet1/{year}")
    public Boolean updateSheet1(@PathVariable Integer year) {
        for (int i = 2; i <= 12; i++) {
            excelService.sheet1Cal(TimeUtil.date(year, i));
        }
        return true;
    }

}
